#!/usr/bin/python
#coding: utf-8

################################
# file: xmlHelp.py             #
# function:                    #
# author: xsseroot@gmail.com   #
# time: 2014-9-3 16:34         #
################################

import os
import sys
from log import loger
from xml.etree import ElementTree

class xmlHelper():
	__xmlDom = None
	__root = None
	__log = None
	isCreated = True
	
	# init 
	def __init__(self,xmlFile,logHandler=None):
		if logHandler:
			self.__log = logHandler			
		else:
			self.__log = loger()
			
		if os.path.exists(xmlFile):
			self.__xmlDom = ElementTree.parse(xmlFile)
			self.__root = self.__xmlDom.getroot()
		else:
			self.__log.Error(("[%s] not found file." % xmlFile))
			isCreated = False
		
	# get children return key,value dictionary
	def GetChildren(self,node,xPath):
		item = node.find(xPath)
		subitem = item.getchildren()
		result = {}
		for elem in subitem:
			result[elem.tag] = elem.text
		return result
		
	# get node list 
	def GetNodeList(self,node,xPath=None):
		if xPath != None:
			return 	node.findall(xPath)
		else:
			return self.__xmlDom.findall(node)	
